﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net.MVC;
using Ext.Net;
using System.Xml.Linq;
using Buffalo.Administration.DAL;

namespace Buffalo.Web.Areas.SMECommon.Controllers
{
    public class HomeController : Controller
    {
        public AjaxStoreResult GetDashboardSchema()
        {
            XElement document = XElement.Load(Server.MapPath("~/Areas/SMECommon/Views/Home/DashboardSchema.xml"));
            var defaultIcon = document.Attribute("defaultIcon") != null ? document.Attribute("defaultIcon").Value : string.Empty;
            var query = from g in document.Elements("group")
                        select new
                        {
                            Title = g.Attribute("title") != null ? GeneralVar.gLanguageHelper.GetLocalizeString(g.Attribute("title").Value) : string.Empty,
                            Items = (from i in g.Elements("item")
                                     select new
                                     {
                                         Title = i.Element("title") != null ? GeneralVar.gLanguageHelper.GetLocalizeString( i.Element("title").Value) : string.Empty,
                                         Icon = i.Element("item-icon") != null ? i.Element("item-icon").Value : defaultIcon,
                                         Accordion = i.Element("accordion-item") != null ? i.Element("accordion-item").Value : string.Empty,
                                         MenuItem = i.Element("menu-item") != null ? i.Element("menu-item").Value : string.Empty
                                     }
                                     )
                        };

            return new AjaxStoreResult(query);
        }

        public ActionResult Index()
        {
            this.ViewData["Username"] = this.HttpContext.User.Identity.Name;
            ConfigDAL configDAL = new ConfigDAL();
            ViewData["ApplicationName"] = configDAL.GetConfigValue("ApplicationName");
            ViewData["Copyright"] = configDAL.GetConfigValue("Copyright");
            ViewData["CompanyUrl"] = configDAL.GetConfigValue("CompanyUrl");
            ViewData["DesignBy"] = configDAL.GetConfigValue("DesignBy");

            return this.View();
        }

        public ActionResult Dashboard()
        {
            List<string> items = new List<string>();

            items.Add("test");

            this.ViewData["Data"] = items;

            return this.View();
        }

        public ActionResult About()
        {
            return this.View();
        }

        public ActionResult Form()
        {
            return this.View();
        }

        public AjaxFormResult SaveForm(string txtName, string txtEmail, string txtComments)
        {

            AjaxFormResult result = new AjaxFormResult();

            result.Script = X.Msg.Alert("Success", "Bug report sent").ToScript();

            return result;
        }
        public ActionResult ReLogin()
        {
            return this.View();
        }
    }
}
